#!/usr/bin/env python
# vi: et sw=2
#
# Gnome PulseAudio applet
# Copyright (C) 2009  Harry Karvonen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Author: Harry Karvonen <harry.karvonen@gmail.com>
#         Aki Rautio <aki.rautio@gmail.com>


import gconf

## Global values
CONF_DIR = '/apps/gnome-pulse-applet'
APP_NAME = 'Gnome PulseAudio Applet'
APP_DESC = ''

## FIXME: Add Description to Gconf items. See gconf-editor...

################################################################################
#
# Config
class Config:

  ##############################################################################
  #
  # Init
  def __init__(self):
    self.client = gconf.client_get_default()
    self.base = CONF_DIR + "/"

    if not self.client.dir_exists(CONF_DIR):
      self.client.add_dir(CONF_DIR,gconf.CLIENT_PRELOAD_NONE)
    
    if not self.checkValuesExist():
      self.__initDefaultConfig()

    return

  ###

  def get(self,data):
    return self.client.get_string(self.base + data)

  ###

  def get_bool(self,data):
    return self.client.get_bool(self.base + data)

  ###

  def save(self,data,value):
    self.client.set_string(self.base + data,str(value))
    return

  ###

  def save_bool(self,data,value):
    self.client.set_bool(self.base + data,int(value))
    return

  ###

  def checkValuesExist(self):
    values = ["layout","sink","distro"]
    for value in values:
      if self.get(value) is None:
        return False
    return True
    
  ###

  def __initDefaultConfig(self):
    self.save("layout", "vertical")
    self.save("sink", "0")
    self.save_bool("icon_use_xml",True)
    self.save("distro",self.getDistro())
    self.save_bool("show_vol_amount",True)

  ###

  def getDistro(self):
    import os    
    import re    
    ## Find distro name from /etc/lsb-release    
    if os.path.isfile('/etc/lsb-release'):
      fp = open('/etc/lsb-release')
      data = fp.readlines()
      fp.close()    
      for row in data:
        if re.search("DISTRIB_ID", row, re.I):
          entries = re.split("=",row)
          distro = entries[1].rstrip().lower()
          if distro != "":         
            return distro
    ## Find distro name from /etc/issue
    if os.path.isfile('/etc/issue'):
      fp = open('/etc/issue')
      data = fp.readlines()
      fp.close()    
      row = data[0]
      entries = re.split(" ",row)
      distro = entries[0].rstrip().lower()
      if distro != "":         
        return distro
    return ""

